package com.lyn.common.config;

import javax.sql.DataSource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.template.source.ClassPathSourceFactory;
import com.lyn.policy.domain._MappingKit;



@Configuration
public class ActiveRecordConfig {
	Logger logger = Logger.getLogger(getClass());
	
    //由于spring中已经注入了DruidDataSource这里直接拿
    @Autowired 
    private DataSource ds;
    
    //默认数据源
    @Bean(initMethod="start", destroyMethod="stop")
    public ActiveRecordPlugin init() {
        ActiveRecordPlugin arp = new ActiveRecordPlugin(ds);
        arp.setContainerFactory(new CaseInsensitiveContainerFactory(true)); //忽略字段大小写
        arp.setShowSql(true);
        arp.setDevMode(true);
        arp.getEngine().setSourceFactory(new ClassPathSourceFactory());
        arp.addSqlTemplate("/sql/all_sqls.sql");
        _MappingKit.mapping(arp);
        return arp;
    }
}
